<?php

namespace App\Console\Commands\OpenShop;

use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageModel;
use App\Service\Guanyuan\CurlService;
use App\Service\OpenShop\Statistical\SignOrderStatisticalService;
use App\Support\OrganizationManageFsRobotSendSupport;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class OpenShopOrderSignStatisticalWeeks extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'open_shop:order_sign_statistical_weeks';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '开店订单每周四驱群通知 .';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 获取所有数据 - 本周新签门店
        $startTimestamp = strtotime(date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)));
        // 本周日
        $endTimestamp = strtotime(date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600))) + 86399;

        // 获取数据模型
        $signOrderService = new SignOrderStatisticalService();
        $signOrderService->setTimeInterval($startTimestamp, $endTimestamp);

        $dataSource = [
            'newWaitLocationList'            => $signOrderService->getNewWaitLocationCount(),
            'waitLocationListTotal'          => $signOrderService->getWaitLocationCount(true),
            'waitLocationListFirstShopTotal' => $signOrderService->getWaitLocationCount(true, true),
            'waitMerchantsLocationList'      => $signOrderService->getWaitMerchantsLocationCount(),
            'waitPreparationsList'           => $signOrderService->getWaitPreparationsCount(),
            'waitPreparationsListTotal'      => $signOrderService->getWaitPreparationsCount(true),
            'gyStatusWeekList'               => $signOrderService->getGYStatusWeek(true),
            'gyAnnualShopData'               => $signOrderService->getGyAnnualShopData(true)
        ];


        $allArea = $signOrderService->getInitAreaList([
            'newSignNum', // 本周新签门店数
            'newWaitLocationCount', // 本周新进选址门店
            'waitLocationNumTotal', // 累计选址中门店数
            'waitLocationNumTotalFirstShopTotal', // 本周首店选址中门店数
            'waitMerchantsLocationNum', // 本周落址门店数
            'waitPreparationsNum', // 本周筹备中的店数
            'waitPreparationsNumTotal', // 累计筹备门店数

            // 观远字段
            'newOpenShopNumMonthGyWeek', // 本月新开-周数据

            'newSignGWNumWeek', // 新签门店数
            'newOpenShopNumWeek', // 本周新开门店数
            'stillOpenShopNumWeek', // 本周仍在营业数据
            'suspendedOpenShopNumWeek', // 暂停门店数
            'sumOpenShopNum', // 累计在营门店数
            'suspendedOpenShopNumMonth', // 暂停营业门店数
            'newOpenTargetNumMonth', // 月度新开门店目标
            'newOpenTargetNumMonthPercent', // 新开完成率
            'newOpenShopNumMonth', // 本月新开门店数
            'gwStillOpenShopTargetNumYear', // 年度累计在营数
        ]);

        foreach ($dataSource as $dsValue) {
            foreach ($dsValue as $dsvValue) {
                $code                            = $dsvValue['provinceCode'];
                $allArea[$code]['templateValue'] = array_merge($allArea[$code]['templateValue'],
                    $dsvValue['templateValue']);
            }
        }

        $resultFormatData = $signOrderService->formatCompanyDataSend($allArea);

        $ytd = SignOrderStatisticalService::getYTD();

        // 推送数据
        foreach ($resultFormatData as $companyDatum) {
            $templateValue = $companyDatum['templateValue'];
            // 本月新开完成率
            $percent1 = bcdiv($templateValue['newOpenShopNumMonthGyWeek'],
                    $templateValue['newOpenTargetNumMonth'] ?: '0.01', 4) * 100;
            // 本年在营完成率
            $percent2 = bcdiv($templateValue['sumOpenShopNum'],
                    $templateValue['gwStillOpenShopTargetNumYear'] ?: '0.01', 4) * 100;
            // 多店未落址门店数 = 累计选址中门店数 - 正在选址中门店数
            $num1        = $templateValue['waitLocationNumTotal'] - $templateValue['waitLocationNumTotalFirstShopTotal'];
            $contentTemp = <<<STR
数据指标
本周新签的门店数: {$templateValue['newSignGWNumWeek']} 家
本周新进选址门店: {$templateValue['newWaitLocationCount']} 家
本周落址的门店数: {$templateValue['waitMerchantsLocationNum']} 家
本周新进筹备门店: {$templateValue['waitMerchantsLocationNum']} 家
本周新开的门店数: {$templateValue['newOpenShopNumWeek']} 家
有效在营的门店数: {$templateValue['stillOpenShopNumWeek']} 家
暂停营业的门店数: {$templateValue['suspendedOpenShopNumWeek']} 家
---------------------------------
累计在营的门店数: {$templateValue['sumOpenShopNum']} 家
正在选址中门店数: {$templateValue['waitLocationNumTotalFirstShopTotal']} 家
多店未落址门店数: {$num1} 家
累计筹备中门店数: {$templateValue['waitPreparationsNumTotal']} 家
---------------------------------
目标达成
本月新开目标: {$templateValue['newOpenTargetNumMonth']} 家
本月新开完成率: {$percent1}%（本月新开门店数/本月新开目标）
本年在营目标: {$templateValue['gwStillOpenShopTargetNumYear']} 家
本年在营完成率: {$percent2}%（累计在营门店数/本年在营目标）
YTD：{$ytd}% （本年已过天数/本年本年总天数）
---------------------------------
辅助数据
累计选址中门店数: {$templateValue['waitLocationNumTotal']}
本月新开门店数: {$templateValue['newOpenShopNumMonthGyWeek']}

STR;

            // 累计选址中门店数: {$templateValue['waitLocationNumTotal']} 家

//            if ($companyDatum['manageId'] != 10) {
//                continue;
//            }
//            $companyDatum['manageId'] = 13;
            OrganizationManageFsRobotSendSupport::send($companyDatum['manageId'],
                '[' . $companyDatum['name'] . '] 周数据 [过程管理]', $contentTemp, 'feishuRobotDataDay');
        }
    }
}
